const rollup = require('rollup')
const path = require('path')

// 根路径
const rootDir = path.resolve('./')

/**
 * 打包脚本
 * @param {} fileName 
 */
async function build(fileName) {
  try {
    // 创建bundle
    const bundle = await rollup.rollup({
      input: path.join(rootDir, 'src', fileName),
      external: ['react'],
    })
    // 写入文件中
    await bundle.write({
      file: path.join(rootDir, 'es', fileName),
      format: 'esm',
      name: 'plugin-dumi-vue',
      globals: {
        'react': 'React'
      }
    })
  } catch (error) {
    console.error(error)
    process.exit(0)
  }
}

// 开始编译上面2个文件
['index.js', 'previewer.js'].forEach(build)